﻿using System;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using HTB.DevFx.Utils;

namespace Maple.Weixin.MP.Helpers
{
    public class WxPayHelper
    {
        public const string DEFAULT_CHARSET = "GB2312";

        /// <summary>
        /// 获取字符串的MD5结果。
        /// </summary>
        /// <param name="encryptionString">要签名的字符串。</param>
        /// <returns>MD5结果（大写）</returns>
        public static string GetMd5(string encryptionString) {
            return GetMd5(encryptionString, "");
        }

        /// <summary>
        /// 获取字符串的MD5结果。
        /// </summary>
        /// <param name="encryptionString">要签名的字符串。</param>
        /// <param name="charset">指定的编码。</param>
        /// <returns>MD5结果（大写）</returns>
        public static string GetMd5(string encryptionString, string charset)
        {
            charset = string.IsNullOrEmpty(charset) ? DEFAULT_CHARSET : charset;
            var encoding = Encoding.GetEncoding(charset);
            return GetMd5(encryptionString, encoding);
        }

        /// <summary>
        /// 获取字符串的MD5结果。
        /// </summary>
        /// <param name="encryptionString">要签名的字符串。</param>
        /// <param name="encoding">指定的编码。</param>
        /// <returns>MD5结果（大写）</returns>
        public static string GetMd5(string encryptionString, Encoding encoding)
        {
            var provider = new MD5CryptoServiceProvider();
            if (encoding == null)
                encoding = Encoding.GetEncoding(DEFAULT_CHARSET);

            var input = encoding.GetBytes(encryptionString);
            var output = provider.ComputeHash(input);

            var retString = BitConverter.ToString(output);
            retString = retString.Replace("-", "").ToUpper();
            return retString;
        }

        public static string UrlEncode(string input) {
            return UrlEncode(input, "");
        }

        public static string UrlEncode(string input, string charset) {
            charset = string.IsNullOrEmpty(charset) ? DEFAULT_CHARSET : charset;
            var encoding = Encoding.GetEncoding(charset);
            return UrlEncode(input, encoding);
        }

        public static string UrlEncode(string input, Encoding encoding) {
            if (string.IsNullOrWhiteSpace(input))
                return string.Empty;

            if (encoding == null)
                encoding = Encoding.GetEncoding(DEFAULT_CHARSET);
            var output = HttpUtility.UrlEncode(input, encoding);
            return output;
        }

        public static string UrlDecode(string input) {
            return UrlDecode(input, "");
        }

        public static string UrlDecode(string input, string charset) {
            charset = string.IsNullOrEmpty(charset) ? DEFAULT_CHARSET : charset;
            var encoding = Encoding.GetEncoding(charset);
            return UrlDecode(input, encoding);
        }

        public static string UrlDecode(string input, Encoding encoding) {
            if (string.IsNullOrWhiteSpace(input))
                return string.Empty;

            if (encoding == null)
                encoding = Encoding.GetEncoding(DEFAULT_CHARSET);
            var output = HttpUtility.UrlDecode(input, encoding);
            return output;
        }

        /// <summary>
        /// 取时间戳生成随机数。
        /// </summary>
        public static uint UnixStamp() {
            var timespan = DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return Convert.ToUInt32(timespan.TotalSeconds);
        }

        /// <summary>
        /// 生成指定长度的随机数。
        /// </summary>
        /// <param name="length">随机数的长度。</param>
        /// <returns>随机字符串的结果。</returns>
        public static string BuildRandomString(int length) {
            return RandomHelper.GetRandomString(length, "0123456789");
        }

        public static string BuildNonce() {
            return RandomHelper.GetRandomString(32);
        }
    }
}
